﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ZR.Admin.WebApi.Extensions;
using ZR.Model.mes.md;
using ZR.Service.mes.md;
using ZR.Service.MES.md;
using ZR.Service.MES.md.IService;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ZR.Admin.WebApi.Controllers.MES.md
{
    [Route("mes/md/BOM")]
    public class MdBOMController :BaseController
    {
        IMdBOMService mdBOMService;
        public MdBOMController(IMdBOMService mdBOMService)
        {
            this.mdBOMService = mdBOMService;
        }

        /// <summary>
        ///  获取bom信息
        /// </summary>
        /// <param name="pageNum">页数</param>
        /// <param name="pageSize">页尺</param>
        /// <param name="productCode">产品code</param>
        /// <param name="productName">产品名称</param>
        /// <returns></returns>
        [HttpGet("list")]
        public IActionResult List(int pageNum, int pageSize, string  productCode = "", string productName = "")
        {
            (int, List<MdBom>) data = mdBOMService.GetAll(productCode, productName, pageNum, pageSize);

            return ToResponse(new ApiResult(200, "success", data));
        }

        /// <summary>
        /// 查询父产品
        /// </summary>
        /// <param name="queryString">productCode码</param>
        /// <returns></returns>
        [HttpGet("queryBOM")]
        public IActionResult List(string queryString="")
        {
            List<MdBom> data = mdBOMService.QueryBOM(queryString);

            return ToResponse(new ApiResult(200, "success", data));
        }

        /// <summary>
        /// 获取全部单位
        /// </summary>
        /// <returns></returns>
        [HttpGet("getunitList")]
        public  IActionResult GetAllunitList()
        {
            List<MdUnit> data = mdBOMService.GetAllunitList();
            return ToResponse(new ApiResult(200, "success", data));
        }


        /// <summary>
        ///  新增BOM数据
        /// </summary>
        /// <param name="bom"></param>
        /// <returns></returns>
        [HttpPost("addBom")]
        public  IActionResult AddBOM([FromBody] MdBom bomItem)
        {
            if(bomItem!=null&&bomItem.Id!=null)
                bomItem.ToCreate(HttpContext);
            if (bomItem.ParentProductId == 0)
                bomItem.ParentProductId = null;
            int result=mdBOMService.AddBom(bomItem);
            return ToResponse(new ApiResult(200, "success", result));
        }

       /// <summary>
       ///  修改bom数据
       /// </summary>
       /// <param name="bomItem"></param>
       /// <returns></returns>
        [HttpPost("updateBom")]
        public IActionResult UpdateBOM([FromBody] MdBom bomItem)
        {
            if (bomItem != null && bomItem.Id != null)
                bomItem.ToUpdate(HttpContext);
            if (bomItem.ParentProductId == 0)
                bomItem.ParentProductId = null;
            int result = mdBOMService.UpdateBOM(bomItem);
            return ToResponse(new ApiResult(200, "success", result));
        }



        [HttpPost("delBom")]
        public IActionResult deleteBOM([FromBody] List<int> ids)
        {
            if (ids != null)
            {
                int result = mdBOMService.deleteBOM(ids.ToArray());
                return ToResponse(result);
            }
            return null;

        }




    }
}
